package com.jxtti.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jxtti.model.District;
import com.jxtti.service.DistrictService;
import com.jxtti.utils.ExceptionUtil;

/**
 * 区域信息相关
 * 
 * @author minshui
 *
 */
@Controller
@RequestMapping("/district")
public class DistrictController {

	private final static Logger LOGGER = Logger
			.getLogger(DistrictController.class);

	@Autowired
	private DistrictService districtService;

	/**
	 * ztree控件获取区域树
	 * 
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/ztree/find/check", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, String>> findByZtreeCheck(
			@RequestParam Map<String, String> data, HttpServletRequest request) {
		return districtService.findByPidZtreeCheck(data);
	}

	/**
	 * 返回easyui数结构的组织架构
	 * 
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findByPidAll", method = RequestMethod.POST)
	@ResponseBody
	public List<Map<String, Object>> findByPidAll(
			@RequestParam Map<String, String> data, HttpServletRequest request) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		try {
			
			if (data.get("orgId")!=null&&!data.get("orgId").equals("")) {
				String[]  orgIds=data.get("orgId").toString().split(",");				
				for(int i=0;i<orgIds.length;i++ ){
					orgIds[i] = orgIds[i].replaceAll(orgIds[i],"'" + orgIds[i] + "'");			
				}			
				String siteids=StringUtils.join(orgIds, ",");		
				data.put("orgId",siteids);	 
			}
			//调用 统一认证
			//DbContextHolder.setDbType(DataSourceConst.SLAVE); //调用service之前只需要设置上下文 调用相应的数据源			
			result = districtService.findByPidAll(data);		
			//DbContextHolder.clearDbType();			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 获取完整的组织架构树
	 * 
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getWholeTree", method = RequestMethod.POST)
	@ResponseBody
	public List<Map<String, String>> getWholeTree(
			@RequestParam Map<String, String> data, HttpServletRequest request) {
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
		try {
			//调用 统一认证
			//DbContextHolder.setDbType(DataSourceConst.SLAVE); //调用service之前只需要设置上下文 调用相应的数据源			
			result = districtService.getWholeTree(data);	
			//DbContextHolder.clearDbType();						
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 获取赣州市 及县 的z-tree结构  bff
	 * 
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getzTreeNodes")
	@ResponseBody
	public List<District> getzTreeNodes(
			@RequestParam Map<String, String> data, HttpServletRequest request) {
		List<District> resultList=new ArrayList<District>();
		//调用 统一认证
		//DbContextHolder.setDbType(DataSourceConst.SLAVE); //调用service之前只需要设置上下文 调用相应的数据源	
		resultList= districtService.getzTreeNodes(data);	
		//DbContextHolder.clearDbType();	
		return resultList;
	}
	
	/**
	 * 更新一条组织架构数据
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(@RequestParam Map<String, String> data,
			HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			int exeCount = districtService.update(data);
			if (exeCount > 0) {
				result.put("success", true);
			} else {
				result.put("success", false);
			}
		} catch (Exception e) {
			result.put("success", false);
			result.put("error", ExceptionUtil.createStackTrackMessage(e));
			// LOGGER.error(e.getCause().getMessage());
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 增加一条组织架构数据
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(@RequestParam Map<String, String> data,
			HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			int exeCount = districtService.add(data);
			if (exeCount > 0) {
				result.put("success", true);
			} else {
				result.put("success", false);
			}
		} catch (Exception e) {
			result.put("success", false);
			result.put("error", ExceptionUtil.createStackTrackMessage(e));
			// LOGGER.error(e.getCause().getMessage());
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 删除一条组织架构数据
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(@RequestParam Map<String, String> data,
			HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			int exeCount = districtService.delete(data);
			if (exeCount > 0) {
				result.put("success", true);
			} else {
				result.put("success", false);
			}
		} catch (Exception e) {
			result.put("success", false);
			result.put("error", ExceptionUtil.createStackTrackMessage(e));
			// LOGGER.error(e.getCause().getMessage());
			e.printStackTrace();
		}
		return result;
	}

}
